<title>用户表</title>
<!--当前位置开始-->
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <script type="text/html" template lay-done="layui.data.updateMainBreadcrumb();">
        </script>
    </div>
</div>
<!--当前位置结束-->
<style>
    /* 重写样式 */
</style>
<script type="text/html" template lay-type="Post" lay-url="{{ layui.setter.apiUrl }}Api/SysUser/GetIndex" lay-done="layui.data.done(d);">
    
</script>
<div class="table-body">
    <table id="LAY-app-SysUser-tableBox" lay-filter="LAY-app-SysUser-tableBox"></table>
</div>

<script type="text/html" id="LAY-app-SysUser-toolbar">
    <div class="layui-form coreshop-toolbar-search-form">
        <div class="layui-form-item">

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="userName" id="userName" placeholder="请输入账号" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="nickName" id="nickName" placeholder="请输入昵称" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" name="phone" id="phone" placeholder="请输入手机号" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-SysUser-search"><i class="layui-icon layui-icon-search"></i>筛选</button>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="LAY-app-SysUser-pagebar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
    </div>
</script>
<script type="text/html" id="LAY-app-SysUser-tableBox-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" data-dropdown="#userTbDelDrop{{d.LAY_NUM}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide" id="userTbDelDrop{{d.LAY_NUM}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除【{{d.userName}}】吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn layui-btn-primary cursor" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal cursor" lay-event="del">确定</a>
        </div>
    </div>
</script>

<script>
    var indexData;
    var debug = layui.setter.debug;
    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d); }

        indexData = d.data;
        layui.use(['index', 'table', 'laydate', 'coredropdown', 'util', 'coreHelper'],
            function () {
                var $ = layui.$
                    , admin = layui.admin
                    , table = layui.table
                    , form = layui.form
                    , laydate = layui.laydate
                    , setter = layui.setter
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , view = layui.view;

                //重载form
                form.render();
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-SysUser-search)',
                    function (data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reloadData('LAY-app-SysUser-tableBox', { where: field });
                    });
                //数据绑定
                table.render({
                    elem: '#LAY-app-SysUser-tableBox',
                    url: layui.setter.apiUrl + 'Api/SysUser/GetPageList',
                    method: 'POST',
                    toolbar: '#LAY-app-SysUser-toolbar',
                    pagebar: '#LAY-app-SysUser-pagebar',
                    className: 'pagebarbox',
                    defaultToolbar: ['filter', 'print', 'exports'],
                    height: 'full-127',//无面包屑127,搜索框189,1行62
                    page: true,
                    limit: 30,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { type: "checkbox", fixed: "left" },
                            { field: 'id', title: '用户id', width: 80, sort: false },
                            { field: 'userName', title: '账号', sort: false, width: 105 },
                            { field: 'nickName', title: '昵称', sort: false },
                            { field: 'phone', title: '手机号码', sort: false, width: 130 },
                            {
                                field: 'roleName', title: '角色', templet: function (d) {
                                    if (d.roles) {
                                        return d.roles.map(function (item) {
                                            return '<span class="layui-badge layui-badge-gray">' + item.roleName + '</span>';
                                        }).join('&nbsp;&nbsp;');
                                    } else {
                                        return "";
                                    }

                                }, sort: true
                            },
                            { field: 'state', title: '状态', sort: false, templet: '#switch_state', width: 105 },
                            { field: 'createTime', title: '注册时间', width: 130, sort: false },
                            { field: 'updateTime', title: '修改时间', width: 130, sort: false },
                            { width: 122, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-SysUser-tableBox-bar' }
                        ]
                    ]
                });
                //监听排序事件
                table.on('sort(LAY-app-SysUser-tableBox)', function (obj) {
                    table.reloadData('LAY-app-SysUser-tableBox', {
                        initSort: obj, //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                        where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                            orderField: obj.field, //排序字段
                            orderDirection: obj.type //排序方式
                        }
                    });
                });
                //头工具栏事件
                table.on('pagebar(LAY-app-SysUser-tableBox)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch (obj.event) {
                        case 'addData':
                            doCreate();
                            break;
                    };
                });
                //监听工具条
                table.on('tool(LAY-app-SysUser-tableBox)',
                    function (obj) {
                        if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });
                //执行创建操作
                function doCreate() {
                    coreHelper.Post("Api/SysUser/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({ shadeClose: false,
                                title: '创建数据',
                                area: ['380px', '400px'],
                                id: 'LAY-popup-SysUser-create',
                                success: function (layero, index) {
                                    view(this.id).render('system/sysuser/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-SysUser-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.emailVerified = field.emailVerified == 'on';
                                                field.deleted = field.deleted == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/SysUser/DoCreate", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reloadData('LAY-app-SysUser-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-SysUser-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行编辑操作
                function doEdit(obj) {
                    coreHelper.Post("Api/SysUser/GetEdit", { id: obj.data.id }, function (e) {
                        if (e.code === 0) {
                            admin.popup({ shadeClose: false,
                                title: '编辑数据',
                                area: ['380px', '400px'],
                                id: 'LAY-popup-SysUser-edit',
                                success: function (layero, index) {
                                    view(this.id).render('system/sysuser/edit', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-SysUser-editForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段

                                                field.emailVerified = field.emailVerified == 'on';
                                                field.deleted = field.deleted == 'on';

                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/SysUser/DoEdit", field, function (e) {
                                                    console.log(e)
                                                    if (e.code === 0) {
                                                        layui.table.reloadData('LAY-app-SysUser-tableBox'); //重载表格
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    })
                                    // 禁止弹窗出现滚动条
                                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-SysUser-editForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                }
                //执行单个删除
                function doDelete(obj) {
                    coreHelper.Post("Api/SysUser/DoDelete", { id: obj.data.id }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        table.reloadData('LAY-app-SysUser-tableBox');
                        layer.msg(e.msg);
                    });
                }

                //监听 表格复选框操作
                layui.form.on('switch(switch_state)', function (obj) {
                    coreHelper.Post("Api/SysUser/DoSetState", { id: this.value, data: obj.elem.checked }, function (e) {
                        if (debug) { console.log(e); } //开启调试返回数据
                        //table.reloadData('LAY-app-SysUser-tableBox');
                        layer.msg(e.msg);
                    });
                });

            });
    };
</script>

<script type="text/html" id="switch_state">
    <input type="checkbox" name="switch_state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="switch_state" {{ d.state==0 ? 'checked' : '' }}>
</script>